﻿using Dynamitey.DynamicObjects;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Common.Model;
using JNPF.Data;
using NPOI.HSSF.Record.Chart;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic.Common.Login
{
    /// <summary>
    /// 登录权限
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.07
    /// </summary>
    public class LoginService : DataFactory, LoginIService
    {

        public async Task<LoginCurrentUserModel> GetAuthorize(bool isAdmin, string userId)
        {
            try
            {
                using (var db = this.Basic)
                {
                    var loginCurrentUserModel = new LoginCurrentUserModel();
                    if (!isAdmin)
                    {
                        var objectId = (await db.GetEntityAsync<UserEntity>(userId)).RoleId.Replace(",", "','");
                        loginCurrentUserModel.moduleList = await db.GetListSqlAsync<ModuleEntity>(string.Format("SELECT * FROM Base_Module WHERE F_Id IN (SELECT F_ItemId FROM Base_Authorize WHERE F_ObjectId IN ('{0}') AND F_ItemType = 'module') and F_EnabledMark = '1' Order by F_SortCode asc", objectId));
                        loginCurrentUserModel.buttonList = await db.GetListSqlAsync<ModuleButtonEntity>(string.Format("SELECT * FROM Base_ModuleButton WHERE F_Id IN (SELECT F_ItemId FROM Base_Authorize WHERE F_ObjectId IN ('{0}') AND F_ItemType = 'button') and F_EnabledMark = '1'  Order by F_SortCode asc", objectId));
                        loginCurrentUserModel.columnList = await db.GetListSqlAsync<ModuleColumnEntity>(string.Format("SELECT * FROM Base_ModuleColumn WHERE F_Id IN (SELECT F_ItemId FROM Base_Authorize WHERE F_ObjectId IN ('{0}') AND F_ItemType = 'column') and F_EnabledMark = '1'  Order by F_SortCode asc", objectId));
                        loginCurrentUserModel.resourceList = await db.GetListSqlAsync<ModuleDataAuthorizeEntity>(string.Format("SELECT * FROM Base_ModuleDataAuthorizeScheme WHERE F_Id IN (SELECT F_ItemId FROM Base_Authorize WHERE F_ObjectId IN ('{0}') AND F_ItemType = 'resource') and F_EnabledMark = '1'  Order by F_SortCode asc", objectId));
                    }
                    else
                    {
                        loginCurrentUserModel.moduleList = await db.GetListSqlAsync<ModuleEntity>(string.Format("SELECT * FROM Base_Module WHERE F_EnabledMark = '{0}' Order by F_SortCode asc", 1));
                        loginCurrentUserModel.buttonList = await db.GetListSqlAsync<ModuleButtonEntity>(string.Format("SELECT * FROM Base_ModuleButton WHERE F_EnabledMark = '{0}' Order by F_SortCode asc", 1));
                        loginCurrentUserModel.columnList = await db.GetListSqlAsync<ModuleColumnEntity>(string.Format("SELECT * FROM Base_ModuleColumn WHERE F_EnabledMark ='{0}' Order by F_SortCode asc", 1));
                        loginCurrentUserModel.resourceList = await db.GetListSqlAsync<ModuleDataAuthorizeEntity>(string.Format("SELECT * FROM Base_ModuleDataAuthorizeScheme WHERE F_EnabledMark ='{0}' Order by F_SortCode asc", 1));

                    }
                    return loginCurrentUserModel;
                }
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }



        public async Task<List<ModuleEntityNew>> GetModule(bool isAdmin, string userId)
        {
            try
            {
                List<ModuleEntityNew> modules = new List<ModuleEntityNew>();
                using (var db = this.Basic)
                {

                    if (!isAdmin)
                    {
                        var objectId = (await db.GetEntityAsync<UserEntity>(userId)).RoleId.Replace(",", "','");
                        modules = await db.GetListSqlAsync<ModuleEntityNew>(string.Format("SELECT * FROM Base_Module WHERE F_CATEGORY='APP' and F_Id IN (SELECT F_ItemId FROM Base_Authorize WHERE F_ObjectId IN ('{0}') AND F_ItemType = 'module') and F_EnabledMark = '1' Order by F_SortCode asc", objectId));

                    }
                    else
                    {
                        modules = await db.GetListSqlAsync<ModuleEntityNew>(string.Format("SELECT * FROM Base_Module WHERE  F_CATEGORY='APP' and F_EnabledMark = '{0}' Order by F_SortCode asc", 1));
                    }
                    return modules;
                }
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }


    }
}
